package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Student;
import cn.edu.ctbu.studentmanage.domain.Teacher;
import cn.edu.ctbu.studentmanage.exception.REnum;
import cn.edu.ctbu.studentmanage.service.StudentService;
import cn.edu.ctbu.studentmanage.service.TeacherService;
import cn.edu.ctbu.studentmanage.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api")
public class LoginApiController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
//
//    @GetMapping("/student")
//    public R student(String name , String classname) throws Exception{
//        Student byNameLikeAndClassname = studentService.findByNameLikeAndClassname(name, classname);
//        return RUtil.success(byNameLikeAndClassname);
//    }

    /**
     * 登录页面./api/user
     * @param httprequest
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    @GetMapping("/user")
    public R user(HttpServletRequest httprequest, String username, String password) throws Exception{
        //判断用户名和密码是否存在
        List<Student> students = studentService.findBySnoAndSpassword(username,password);
        List<Teacher> teachers = teacherService.findByTnoAndTpassword(username,password);
        if(students.size()>0){
            //人如果存在，写入Session
            HttpSession session = httprequest.getSession();
            session.setAttribute("userid",students.get(0).getSid());
            session.setAttribute("teacheruserid",null);
            session.setAttribute("user",students.get(0));

            return RUtil.success(null);//成功
        } else if (teachers.size()>0) {
            HttpSession session = httprequest.getSession();
            session.setAttribute("teacheruserid",teachers.get(0).getTid());
            session.setAttribute("userid",null);
            session.setAttribute("user",teachers.get(0));

            return RUtil.successteacher(null);//成功
        } else{
            return RUtil.error(REnum.LOGIN_ERR);//不存在用户
        }
    }
//    @GetMapping("/teacheruser")
//    public int teacheruser(HttpServletRequest httprequest, String username, String password){
//        //判断用户名和密码是否存在
//        List<Teacher> teachers = teacherService.findByTnoAndTpassword(username,password);
//
//        if(teachers.size()>0){
//            //人如果存在，写入Session
//            HttpSession session = httprequest.getSession();
//            session.setAttribute("teacheruserid",teachers.get(0).getTid());
//            session.setAttribute("userid",null);
//            session.setAttribute("user",teachers.get(0));
//
//            return 1;//成功
//        }else{
//            return -1;//不存在用户
//        }
//    }

    /**
     * 判断学生是否登录./api/nginxlogin
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/nginxlogin")
    public R nginxlogin(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();

        if(session.getAttribute("userid") == null){
            //没有登录，请先去登录
//            return -1;
            return RUtil.error(REnum.LOGIN_ERR);
        }else {
//            return 10;
            return RUtil.success(null);
        }

    }

    /**
     * 判断教师是否登录./api/nginxteacherlogin
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/nginxteacherlogin")
    public R nginxteacherlogin(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();


        if(session.getAttribute("teacheruserid") == null){
            //没有登录，请先去登录
//            return -1;
            return RUtil.error(REnum.LOGIN_ERR);
        }else {
//            return 1;
            return RUtil.successteacher(null);
        }
    }
}
